package com.weixin.project.Controller;

import com.alibaba.fastjson.JSONObject;
import com.weixin.project.Annotation.VipAccess;
import com.weixin.project.Entity.User;
import com.weixin.project.Service.TestService;
import com.weixin.project.Util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
public class TestController {
    @Autowired
    TestService testService;
    @GetMapping("/test/login")
    String login(@RequestParam("id") Integer id){
        return testService.login(id);
    }

    @GetMapping("/test/current_user")
    JSONObject getCurrentUser(HttpServletRequest request){
        User user = ((User) request.getAttribute("current_user"));
        System.out.println(user.getName());
        return ResponseUtil.success("");
    }

    @VipAccess
    @GetMapping("/test/vip_info")
    JSONObject getVipInfo(HttpServletRequest request,
                          @RequestParam("id") Integer id){
        User user = ((User) request.getAttribute("current_user"));
        System.out.println("id:" + id);
        System.out.println("user_name:" + user.getName());
        return ResponseUtil.success();
    }

    @GetMapping("/test/addpractices")
    String addPractices(){
        System.out.println("success");
        return "success";
    }

    @GetMapping("/header")
    String testHeader(@RequestHeader("name") String name ){
        System.out.println("====================================");
        System.out.println("header: "+ name);
        System.out.println("====================================");
        return "name:" + name;
    }

    @GetMapping("/hello")
    String hello(){
        return "hello";
    }
}
